//
//  UIViewController+Init.m
//  SHDoctor
//
//  Created by Mac mini on 15/10/10.
//  Copyright © 2015年 ShiHeng. All rights reserved.
//

#import "UIViewController+Init.h"

#import <objc/runtime.h>

@implementation UIViewController(Init)

+ (void)load {
    
    Method viewDidLoad = class_getInstanceMethod([self class], NSSelectorFromString(@"viewDidLoad"));
    Method init_viewDidLoad = class_getInstanceMethod([self class], @selector(init_viewDidLoad));
    method_exchangeImplementations(viewDidLoad, init_viewDidLoad);
}

- (void)init_viewDidLoad {
    [self init_viewDidLoad];
    
    if ([self.parentViewController isKindOfClass: [UITabBarController class]]) {
        self.navigationItem.leftBarButtonItem = nil;
    }
    else if ([self.parentViewController isKindOfClass: [UINavigationController class]]) {
        
        if (self.navigationController.viewControllers.count > 1 || [self showBackButton]) {
            self.navigationItem.hidesBackButton = true;
            UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed:@"navigation_bar_back"] style: UIBarButtonItemStyleBordered target: self action: @selector(kk_back:)];
            backItem.tintColor = [UIColor whiteColor];
            
            self.navigationItem.leftBarButtonItem = backItem;
        }
        else {
            self.navigationItem.leftBarButtonItems = nil;
        }
    }
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = false;
//    self.view.backgroundColor = [UIColor colorWithHexString: @"#f7f7f7"];
    
    //  取消键盘事件
    UITapGestureRecognizer *keyboardTapper = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(init_keyboardTap:)];
    keyboardTapper.cancelsTouchesInView = false;
    [self.view addGestureRecognizer: keyboardTapper];
}

#pragma mark - Private methods

- (void)init_keyboardTap:(UITapGestureRecognizer*)tapper {
    
    [self.view endEditing: true];
}

#pragma mark - Setter methods

@dynamic showBackButton;

static const void *UIViewControllerShowBackButtonValue = &UIViewControllerShowBackButtonValue;

- (void)setShowBackButton:(BOOL)showBackButton {
    
    objc_setAssociatedObject(self, UIViewControllerShowBackButtonValue, [NSNumber numberWithBool: showBackButton], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)showBackButton {
    
    return [objc_getAssociatedObject(self, UIViewControllerShowBackButtonValue) boolValue];
}

#pragma mark - Private methods

- (void)kk_back:(UIBarButtonItem *)item {
    
    if (self.navigationController.viewControllers.count != 1) {
        [self.navigationController popViewControllerAnimated: true];
    }
    else {
        [self.navigationController dismissViewControllerAnimated: true completion: nil];
    }
}

#pragma mark - Class methods

+ (instancetype)viewControllerFromXIB {
    
    return [self viewControllerFromXIBWithNibName: NSStringFromClass(self.class)];
}

+ (instancetype)viewControllerFromXIBWithNibName:(NSString *)nibName {
    
    if (nibName.length == 0) {
        nibName = NSStringFromClass(self.class);
    }
    
    UIViewController *viewController = [[super alloc] initWithNibName: nibName bundle: [NSBundle mainBundle]];
    return viewController;
}

+ (instancetype)viewControllerFromStoryboard {
    return [self viewControllerFromStoryboardWithIdentifier: NSStringFromClass(self.class)];
}

+ (instancetype)viewControllerFromStoryboardWithIdentifier:(NSString *)identifier {
    return [self viewControllerFromStoryboardWithStoryboardName: @"Main" identifier: identifier];
}

+ (instancetype)viewControllerFromStoryboardWithStoryboardName:(NSString *)storyboardName identifier:(NSString *)identifier {
    
    if (storyboardName.length == 0) {
        storyboardName = @"Main";
    }
    if (identifier.length == 0) {
        identifier = NSStringFromClass(self.class);
    }
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName: storyboardName bundle: [NSBundle mainBundle]];
    
    UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier: identifier];
    
    return viewController;
}

@end
